package com.chuanke.ckfamily.service.finance.impl;

import com.chuanke.ckfamily.dao.common.model.IncomeConf;
import com.chuanke.ckfamily.service.base.FailCode;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.IncomeConfService;
import com.chuanke.ckfamily.service.common.impl.RedisLockUtil;
import com.chuanke.ckfamily.service.constant.IncomeChannel;
import com.chuanke.ckfamily.service.exception.CFException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chuanke.ckfamily.dao.finance.UserDayIncomeDao;
import com.chuanke.ckfamily.dao.finance.model.UserDayIncome;
import com.chuanke.ckfamily.service.finance.UserDayIncomeService;
import com.weijie.core.BaseBiz;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserDayIncomeServiceImpl extends BaseBiz<UserDayIncome, UserDayIncomeDao> implements UserDayIncomeService {
	private static final Logger log = LoggerFactory.getLogger(UserDayIncomeServiceImpl.class);
	
	@Autowired
	private IncomeConfService incomeConfService;

	@Autowired
	private CacheService cacheService;

	@Override
	public void addUserIncome(Long channel, Long uid, Long money, String date) throws CFException {
		if (uid == null || channel == null) {
			throw new CFException(FailCode.APP, "参数为空");
		}
		// 参数正确 更新用户日收益表
		this.updateUserDayIncome(channel, uid, money, date);
	}

	@Override
	public UserDayIncome findUserDayIncome(Long uid, String date) throws CFException {
		if (uid == null || date == null) {
			throw new CFException(FailCode.APP, "参数为空");
		}
		Map<String, Object> param = new HashMap<>(2);
		param.put("uid", uid);
		param.put("date", date);
		return this.baseDao.findUserDayIncomeByUid(param);
	}

	private void updateUserDayIncome(Long channel, Long uid, Long money, String date) throws CFException {
		// 根据操作类型找到对应的奖励
		IncomeConf ic = incomeConfService.findById(channel);
		if (ic == null) {
			throw new CFException(FailCode.APP, "操作类型参数有误");
		}

		RedisLockUtil lockUtil = null;
		try {
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.user_dayincome_.name() + uid + "_" + date);
			UserDayIncome dayIncome = findDayUserIncome(uid, date);
			if (ic.getId() == IncomeChannel.INVITE_CHANNEL.getType()) {
				dayIncome.setAll_money(dayIncome.getAll_money() + money);
				dayIncome.setInvate_money(dayIncome.getInvate_money() + money);
				dayIncome.setInvate_total(dayIncome.getInvate_total() + 1);

			} else if (ic.getId() == IncomeChannel.ACTIVE_CHANNEL.getType()) {
				if (dayIncome.getShare_total() == 0) {
					dayIncome.setShare_money(dayIncome.getShare_money() + money);
					dayIncome.setAll_money(dayIncome.getAll_money() + money);
				}
				dayIncome.setShare_total(dayIncome.getShare_total() + 1);

			}

			save(dayIncome);

		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			if (lockUtil != null) {
				lockUtil.unlock();
			}
		}
	}

	private UserDayIncome findDayUserIncome(Long uid, String date) {
		Map<String, Object> param = new HashMap<>(2);
		param.put("uid", uid);
		param.put("date", date);
		UserDayIncome userDayIncome = this.baseDao.findUserDayIncomeByUid(param);
		if (userDayIncome == null) {
			userDayIncome = new UserDayIncome();
			userDayIncome.setUid(uid);
			userDayIncome.setI_date(date);
			userDayIncome.setInvate_total(0); // 第一次收益
			userDayIncome.setInvate_money(0L);
			userDayIncome.setShare_total(0);
			userDayIncome.setShare_money(0l);
			userDayIncome.setAll_money(0l);
			userDayIncome.setCreate_time(new Timestamp(System.currentTimeMillis()));
		}

		return userDayIncome;
	}

	public Boolean isDayUserShared(Long uid, String date) {
		Map<String, Object> param = new HashMap<>(2);
		param.put("uid", uid);
		param.put("date", date);
		UserDayIncome userDayIncome = baseDao.findUserDayIncomeByUid(param);
		log.debug("isDayUserShared======>param={},result={}",param,userDayIncome);
		return userDayIncome != null
				&& userDayIncome.getShare_total() != null
				&& userDayIncome.getShare_total() > 0;
	}
}